//leetcode:876.链表的中间结点


/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
//遍历一次的方法
//总体思想就是fast每次走两个结点，slow每次走一个结点
class Solution {
    public ListNode middleNode(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
}


//遍历链表两次的方法，不宜采用
/*class Solution {
    public ListNode middleNode(ListNode head) {
        //1.求链表长度
        int sz = 0;
        ListNode tmp = head;
        while(tmp != null) {
            sz++;
            tmp = tmp.next;
        }
        //2.返回中间结点
        int mid = sz/2;
        ListNode middle = head;
        while(mid > 0) {
            middle = middle.next;
            mid--;
        }
        return middle;
    }
}*/